package com.example.wechatdaka.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechatdaka.entity.User;
import com.example.wechatdaka.entity.UserWechat;
import com.example.wechatdaka.service.UserWechatService;
import com.example.wechatdaka.utils.Result;
import com.example.wechatdaka.utils.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "微信绑定管理模块")
@RestController
@RequestMapping("/wechatBind")
public class WechatBindController {
    @Autowired
    private UserWechatService userWechatService;
    @ApiOperation(value = "查询所有已绑定学号")
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public Result queryAllUserWechat(){
        List<UserWechat> userWechatList = userWechatService.queryAllUserWechat();
        return new Result(Result.SUCCESS,userWechatList,"SUCCESS");
    }
    @ApiOperation(value = "分页查询所有已绑定学号")
    @RequestMapping(value = "/indexByPage",method = RequestMethod.GET)
    public JsonResult queryAllByPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize) {
        Page<UserWechat> page = new Page<>(currentPage, pagesize);
        IPage<UserWechat> userWechatIPage = userWechatService.queryAllUserWechatByPage(page);
        List<UserWechat> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "根据id删除已绑定学号")
    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    public Result deleteById(@RequestBody String id){
        boolean removeById = userWechatService.removeById(id);
        if (removeById){
            return new Result(Result.SUCCESS,null,"SUCCESS");
        }
        return new Result(Result.ERROR,null,"ERROR");
    }
    @ApiOperation(value = "根据班级查询")
    @RequestMapping(value = "/queryByClazz",method = RequestMethod.POST)
    public Result queryByClazz(@RequestBody String id){
        List<UserWechat> userWechatList = userWechatService.queryByClazz(id);
        return new Result(Result.SUCCESS,userWechatList,"SUCCESS");
    }
    @ApiOperation(value = "分页根据班级查询")
    @RequestMapping(value = "/queryByClazzPage",method = RequestMethod.GET)
    public JsonResult queryByClazzPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String id) {
        Page<UserWechat> page = new Page<>(currentPage, pagesize);
        IPage<UserWechat> userWechatIPage = userWechatService.queryByClazzPage(page,id);
        List<UserWechat> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "根据学号查询")
    @RequestMapping(value = "/queryByUserNumber",method = RequestMethod.POST)
    public Result queryByUserNumber(@RequestBody String id){
        UserWechat userWechat = userWechatService.queryByUserNumber(id);
        return new Result(Result.SUCCESS,userWechat,"SUCCESS");
    }


}
